package binaria;

import iterator.Iterator;
import tree.AbstractTree;
import tree.PreOrder;
import tree.PrePostVisitor;
import tree.Tree;
import visitor.Visitor;
import wrapper.Comparable;

public class BinaryTree extends AbstractTree {
	protected Object key;
	protected BinaryTree left;
	protected BinaryTree right;

	public BinaryTree(Object key, BinaryTree left, BinaryTree right) {
		this.key = key;
		this.left = left;
		this.right = right;
	}

	public BinaryTree() {
		this(null, null, null);
	}

	public BinaryTree(Object key) {
		this(key, new BinaryTree(), new BinaryTree());
	}

	public void depthFirstTraversal(PrePostVisitor visitor) {
		if (!isEmpty()) {
			visitor.preVisit(key);
			left.depthFirstTraversal(visitor);
			visitor.inVisit(key);
			right.depthFirstTraversal(visitor);
			visitor.postVisit(key);
		}
	}

	@Override
	public Object getKey() {
		// TODO Auto-generated method stub
		return null;
	}

	public BinaryTree getLeft() {
		return left;
	}

	public void attachLeft(BinaryTree left) {
		this.left = left;
	}

	public BinaryTree getRight() {
		return right;
	}

	public void attachRight(BinaryTree right) {
		this.right = right;
	}

	public void setKey(Object key) {
		this.key = key;
	}

	@Override
	public Tree getSubtree(int i) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isLeaf() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int getDegree() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getHeight() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void purge() {
		key = null;
		left = null;
		right = null;
	}

	@Override
	public Iterator getIterator() {
		// TODO Auto-generated method stub
		return null;
	}

	protected int compareTo(Comparable object) {
		BinaryTree arg = (BinaryTree) object;
		if (isEmpty())
			return arg.isEmpty() ? 0 : -1;
		else if (arg.isEmpty())
			return 1;
		else {
			int result = ((Comparable) getKey()).compare((Comparable) arg
					.getKey());
			if (result == 0)
				result = getLeft().compareTo(arg.getLeft());
			if (result == 0)
				result = getRight().compareTo(arg.getRight());
			return result;
		}
	}
}